﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace TimeTools {
    public partial class TimeConversionControl : UserControl {
        private bool _inUpdateNumber;

        public TimeConversionControl() {
            InitializeComponent();
        }

        private void _years_TextChanged(object sender, EventArgs e) {
            try {
                UpdateTime(Convert.ToDouble(_years.Text) * 365 * 24 * 60 * 60 * 1000, true);
                _years.ForeColor = Color.Black;
            } catch {
                _years.ForeColor = Color.Red;
            }
        }

        private void _months_TextChanged(object sender, EventArgs e) {
            try {
                UpdateTime(Convert.ToDouble(_months.Text) * 30 * 24 * 60 * 60 * 1000, true);
                _months.ForeColor = Color.Black;
            } catch {
                _months.ForeColor = Color.Red;
            }
        }

        private void _weeks_TextChanged(object sender, EventArgs e) {
            try {
                UpdateTime(Convert.ToDouble(_weeks.Text) * 7 * 24 * 60 * 60 * 1000, true);
                _weeks.ForeColor = Color.Black;
            } catch {
                _weeks.ForeColor = Color.Red;
            }
        }

        private void _days_TextChanged(object sender, EventArgs e) {
            try {
                UpdateTime(Convert.ToDouble(_days.Text) * 24 * 60 * 60 * 1000, true);
                _days.ForeColor = Color.Black;
            } catch {
                _days.ForeColor = Color.Red;
            }
        }

        private void _hours_TextChanged(object sender, EventArgs e) {
            try {
                UpdateTime(Convert.ToDouble(_hours.Text) * 60 * 60 * 1000, true);
                _hours.ForeColor = Color.Black;
            } catch {
                _hours.ForeColor = Color.Red;
            }
        }

        private void _minutes_TextChanged(object sender, EventArgs e) {
            try {
                UpdateTime(Convert.ToDouble(_minutes.Text) * 60 * 1000, true);
                _minutes.ForeColor = Color.Black;
            } catch {
                _minutes.ForeColor = Color.Red;
            }
        }

        private void _seconds_TextChanged(object sender, EventArgs e) {
            try {
                UpdateTime(Convert.ToDouble(_seconds.Text) * 1000, true);
                _seconds.ForeColor = Color.Black;
            } catch {
                _seconds.ForeColor = Color.Red;
            }
        }

        private void _milliseconds_TextChanged(object sender, EventArgs e) {
            try {
                UpdateTime(Convert.ToDouble(_milliseconds.Text), true);
                _milliseconds.ForeColor = Color.Black;
            } catch {
                _milliseconds.ForeColor = Color.Red;
            }
        }

        private void _time_TextChanged(object sender, EventArgs e) {
            try {
                TimeSpan t = TimeSpan.Parse(_time.Text);
                UpdateTime((long)t.TotalMilliseconds, false);
                _time.ForeColor = Color.Black;
            } catch {
                _time.ForeColor = Color.Red;
            }
        }

        private void UpdateTime(double millis, bool updateTime) {
            if (!_inUpdateNumber) {
                try {
                    _inUpdateNumber = true;
                    _milliseconds.Text = Convert.ToString(millis);
                    _seconds.Text = Convert.ToString(millis / 1000.0);
                    _minutes.Text = Convert.ToString(millis / 60.0 / 1000.0);
                    _hours.Text = Convert.ToString(millis / 60.0 / 60.0 / 1000.0);
                    _days.Text = Convert.ToString(millis / 24.0 / 60.0 / 60.0 / 1000.0);
                    _weeks.Text = Convert.ToString(millis / 7.0 / 24.0 / 60.0 / 60.0 / 1000.0);
                    _months.Text = Convert.ToString(millis / 30.0 / 24.0 / 60.0 / 60.0 / 1000.0);
                    _years.Text = Convert.ToString(millis / 365.0 / 24.0 / 60.0 / 60.0 / 1000.0);
                    if (updateTime) {
                        TimeSpan t = TimeSpan.FromMilliseconds(millis);
                        _time.Text = t.ToString();
                    }
                } finally {
                    _inUpdateNumber = false;
                }
            }
        }
    }
}
